﻿using MediatR;
using W.Purchase.API.Application.Command.FinanceTypeCommand;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.FinanceTypeCommandHandler
{
    public class FinanceTypeUpdateCommandHandler:IRequestHandler<FinanceTypeUpdateCommand,int>
    {
        private readonly IFinanceTypesRepository _financeTypesRepository;
        public FinanceTypeUpdateCommandHandler(IFinanceTypesRepository financeTypesRepository)
        {
            _financeTypesRepository = financeTypesRepository;
        }

        public async Task<int> Handle(FinanceTypeUpdateCommand request, CancellationToken cancellationToken)
        {
            var list = await _financeTypesRepository.GetAllAsaync();
            var plist=list.Where(c=>c.FinanceTypeId==request.FinanceTypeId).FirstOrDefault();
            if (plist != null)
            {
                plist.FinanceTypeId = request.FinanceTypeId;
                plist.FinanceTypeNo = request.FinanceTypeNo;
                plist.FinanceTypeName = request.FinanceTypeName;
                plist.FinanceTyperemark = request.FinanceTyperemark;
                plist.office_id=request.office_id;
                plist.user_id=request.user_id;
                plist.CreateName = request.CreateName;
                plist.CreateTime = request.CreateTime;
                plist.UpdateName = request.UpdateName;
                plist.UpdateTime = DateTime.Now;
                plist.IsDelete = request.IsDelete;
            }
            return await _financeTypesRepository.Update(plist);
        }
    }
}
